package com.itcast.donkey.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.activity.DetailedTopicActivity;
import com.itcast.donkey.modles.TopicModles;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by lanouhn on 16/8/25.
 */
public class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.TopicViewHolder> {

    private Context context;
    private ArrayList<TopicModles> modlesArrayList;

    public TopicAdapter(Context context, ArrayList<TopicModles> modlesArrayList) {
        this.context = context;
        this.modlesArrayList = modlesArrayList;
    }

    @Override
    public TopicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_activity_topic, null);
        TopicViewHolder holder = new TopicViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(TopicViewHolder holder, final int position) {

        Picasso.with(context).load(modlesArrayList.get(position).getImage_url()).into(holder.ivItemTopicBg);
        holder.tvItemTopicName.setText(modlesArrayList.get(position).getName());
        holder.tvItemTopicTitle.setText(modlesArrayList.get(position).getTitle());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, DetailedTopicActivity.class);
                intent.putExtra("id",modlesArrayList.get(position).getId());
                context.startActivity(intent);

            }
        });
    }

    @Override
    public int getItemCount() {
        return modlesArrayList.size();
    }

    public class TopicViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivItemTopicBg;
        private TextView tvItemTopicName;
        private TextView tvItemTopicTitle;

        public TopicViewHolder(View itemView) {
            super(itemView);
            ivItemTopicBg = (ImageView) itemView.findViewById(R.id.iv_item_topic_bg);
            tvItemTopicName = (TextView) itemView.findViewById(R.id.tv_item_topic_name);
            tvItemTopicTitle = (TextView) itemView.findViewById(R.id.tv_item_topic_title);
        }
    }
}
